package com.jingan.nursinghome.regulatory.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.base.BaseView;
import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/25.
 */

public class CircleView extends BaseView implements View.OnTouchListener{
    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    Bitmap s,d;
    private void init(){
        s= AssetsUtils.getInstance().getBitmapFromAssets("图标/圆形选择.png",0,true);
        d= AssetsUtils.getInstance().getBitmapFromAssets("图标/圆形默认.png",0,true);
        s= ProportionUtils.getInstance().adapterBitmap(s);
        d= ProportionUtils.getInstance().adapterBitmap(d);
        //setOnTouchListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(s.getWidth()*2,s.getHeight()*2);
    }

    boolean state=false;

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
        invalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if(state){
            canvas.drawBitmap(s,(getWidth()-s.getWidth())/2,(getHeight()-s.getHeight())/2,paint);
        }else {
            canvas.drawBitmap(d,(getWidth()-s.getWidth())/2,(getHeight()-s.getHeight())/2,paint);
        }
    }

    GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(getContext(),new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            setState(!state);
            return super.onSingleTapConfirmed(e);
        }
    });

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gestureDetectorCompat.onTouchEvent(event);
        return true;
    }
}
